package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqNotice;
import cloud.maque.admin.mapper.MqNoticeMapper;
import cloud.maque.admin.service.MqNoticeService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author 4color
 * @description 公告
 * @date 2023-03-17
 */
@Service
public class MqNoticeServiceImpl extends ServiceImpl<MqNoticeMapper, MqNotice> implements MqNoticeService {


    @Override
    public MqNotice insert(MqNotice mqNotice) {
        Assert.hasText(mqNotice.getTitle(), "标题不能为空");
        save(mqNotice);
        return mqNotice;
    }

    @Override
    public Boolean delete(String noticeId, String tenantId) {
        QueryWrapper<MqNotice> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqNotice::getNoticeId, noticeId);
        wrapper.lambda().eq(MqNotice::getTenantId, tenantId);
        return remove(wrapper);
    }

    @Override
    public PageInfo<MqNotice> page(PaginateDataResquest<MqNotice> pageReq) {

        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqNotice> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTitle())) {
            wrapper.lambda().like(MqNotice::getTitle, pageReq.getData().getTitle());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqNotice::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqNotice::getCreateAt);
        wrapper.lambda().orderByAsc(MqNotice::getNoticeId);
        List<MqNotice> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqNotice> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }
}
